﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite;

//для работы с почтой
using System.Net;
using System.Net.Mail;

namespace ProductDB
{
    class public_method
    {
        //для шифрования
        public string StringToSHA512(string str, string salt)
        {
            string sha512str = "";

            byte[] strBytes = Encoding.UTF8.GetBytes(str + salt.ToLower().Trim());
            using (SHA512 sha512 = SHA512.Create())
            {
                byte[] hashData = sha512.ComputeHash(strBytes);
                for (int i = 0; i < hashData.Length; i++)
                {
                    sha512str += hashData[i].ToString("X2");
                }
            }
            return sha512str;
        }

        //для почты
        int codeReg;
        public void mailaddressStr(string ServerMailAddress, string ServerPass, string Name, string UserMailAddress, string HostName)
        {
            try
            {
                Random random = new Random();
                codeReg = random.Next(0, 10000);
                MailAddress from = new MailAddress(ServerMailAddress, Name);
                MailAddress to = new MailAddress(UserMailAddress);
                using (MailMessage mail = new MailMessage(from, to))
                {
                    using (SmtpClient smtp = new SmtpClient())
                    {
                        mail.Subject = "Подтверждение адреса электронной почты";
                        mail.Body = "Код подтверждения почты: " + codeReg.ToString("D4");
                        smtp.Host = HostName;
                        smtp.Port = 587;
                        smtp.EnableSsl = true;
                        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new NetworkCredential(ServerMailAddress, ServerPass);
                        smtp.Send(mail);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Произошла ошибка: " + ex);
            }
        }

        //проверка на существование емайла
        public bool EmailExists(string email, SqlConnection sqlConnection)
        {
            bool exists = false;

            try
            {
                SqlCommand sqlCommand = new SqlCommand(
                    "SELECT email FROM users WHERE email = @email", sqlConnection);

                sqlCommand.Parameters.AddWithValue("@email", email);

                SqlDataReader dataReader = sqlCommand.ExecuteReader();

                exists = dataReader.Read();
                dataReader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }           
            return exists;

        }
        public bool EmailExists(string email, SQLiteConnection sqliteConnection)
        {
            bool exists = false;

            try
            {
                SQLiteCommand sqlCommand = new SQLiteCommand(
                    "SELECT email FROM users WHERE email = @email", sqliteConnection);

                sqlCommand.Parameters.Add("@email", DbType.String).Value = email;

                SQLiteDataReader dataReader = sqlCommand.ExecuteReader();

                exists = dataReader.Read();
                dataReader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return exists;
        }

        //взять из базы емайл
        public string GetEmail(string email, SqlConnection sqlConnection)
        {
            string userEmail = string.Empty;

            try
            {
                SqlCommand sqlCommand = new SqlCommand(
                    "SELECT email FROM users WHERE email = @email", sqlConnection);

                sqlCommand.Parameters.AddWithValue("@email", email);

                SqlDataReader dataReader = sqlCommand.ExecuteReader();

                if (dataReader.Read())
                {
                    userEmail = dataReader["email"].ToString();
                }
                dataReader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return userEmail;
        }
        public string GetEmail(string email, SQLiteConnection sqliteConnection)
        {
            string userEmail = string.Empty;

            try
            {
                SQLiteCommand sqlCommand = new SQLiteCommand(
                    "SELECT email FROM users WHERE email = @email", sqliteConnection);

                sqlCommand.Parameters.Add("@email", DbType.String).Value = email;

                SQLiteDataReader dataReader = sqlCommand.ExecuteReader();

                if (dataReader.Read())
                {
                    userEmail = dataReader["email"].ToString();
                }
                dataReader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return userEmail;
        }

        //взять из базы пароль
        public string GetPassword(string email, SqlConnection sqlConnection)
        {
            string userPassword = string.Empty;

            try
            {
                SqlCommand sqlCommand = new SqlCommand(
                    "SELECT pass FROM users WHERE email = @email", sqlConnection);

                sqlCommand.Parameters.AddWithValue("@email", email);

                SqlDataReader dataReader = sqlCommand.ExecuteReader();

                if (dataReader.Read())
                {
                    userPassword = dataReader["pass"].ToString();
                }
                dataReader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return userPassword;
        }
        public string GetPassword(string email, SQLiteConnection sqliteConnection)
        {
            string userPassword = string.Empty;

            try
            {
                SQLiteCommand sqlCommand = new SQLiteCommand(
                    "SELECT pass FROM users WHERE email = @email", sqliteConnection);

                sqlCommand.Parameters.Add("@email", DbType.String).Value = email;

                SQLiteDataReader dataReader = sqlCommand.ExecuteReader();

                if (dataReader.Read())
                {
                    userPassword = dataReader["pass"].ToString();
                }
                dataReader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return userPassword;
        }

        //запрос о роли в БД
        public int GetRole(string email, SqlConnection sqlConnection)
        {
            int role = 0;
            try
            {
                string query = "SELECT role FROM users WHERE email = @email";
                SqlCommand command = new SqlCommand(query, sqlConnection);
                command.Parameters.AddWithValue("@email", email);
                role = (int)command.ExecuteScalar();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return role;
        }
        public int GetRole(string email, SQLiteConnection sqliteConnection)
        {
            int role = 0;
            try
            {
                string query = "SELECT role FROM users WHERE email = @email";
                SQLiteCommand command = new SQLiteCommand(query, sqliteConnection);
                command.Parameters.Add("@email", DbType.String).Value = email;
                role = Convert.ToInt32(command.ExecuteScalar());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return role;
        }

        //взять из базы Фамилие и инициалы
        public string GetName(string email, SqlConnection sqlConnection)
        {
            string userName = string.Empty;

            try
            {
                SqlCommand sqlCommand = new SqlCommand(
                    "SELECT Name, Surname, Patronymic FROM users WHERE email = @email", sqlConnection);

                sqlCommand.Parameters.AddWithValue("@email", email);

                SqlDataReader dataReader = sqlCommand.ExecuteReader();

                if (dataReader.Read())
                {
                    string name = dataReader["Name"].ToString();
                    string surname = dataReader["Surname"].ToString();
                    string patronymic = dataReader["Patronymic"].ToString();

                    userName = (surname + " " + name.Substring(0, 1) + "." + patronymic.Substring(0, 1)).ToString() + ".";
                }
                dataReader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return userName;
        }
        public string GetName(string email, SQLiteConnection sqliteConnection)
        {
            string userName = string.Empty;

            try
            {
                SQLiteCommand sqlCommand = new SQLiteCommand(
                    "SELECT Name, Surname, Patronymic FROM users WHERE email = @email", sqliteConnection);

                sqlCommand.Parameters.Add("@email", DbType.String).Value = email;

                SQLiteDataReader dataReader = sqlCommand.ExecuteReader();

                if (dataReader.Read())
                {
                    string name = dataReader["Name"].ToString();
                    string surname = dataReader["Surname"].ToString();
                    string patronymic = dataReader["Patronymic"].ToString();

                    userName = (surname + " " + name.Substring(0, 1) + "." + patronymic.Substring(0, 1)).ToString() + ".";
                }
                dataReader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return userName;
        }


    }
    }
